//
//  ApplicationManager.swift
//  ltsp
//
//  Created by jyvyi_ios on 2022/5/24.
//

import Foundation

enum SettingType : String{
    case photo = "相册权限被禁用，是否去设置->隐私->相册中授予该权限?"
    case location = "位置权限被禁用，是否去设置->隐私->位置中授予该权限?"
    case microPhone = "麦克风权限禁用,是否去设置->隐私->麦克风中授予该权限?"
    
    func handler() -> ((UIAlertAction)->Void)?{
        
        let handler : ((UIAlertAction)->Void) = { alertAction in
            
            ApplicationManager.openSetting()
        }
        return handler
    }
}

class ApplicationManager{
    
    /// 打开设置
    class func openSetting(){
        
        guard let url = URL.init(string: UIApplication.openSettingsURLString) else {
            return
        }
        UIApplication.shared.open(url)
    }
    
    /// 拨打电话
    /// - Parameter phone: 电话号码
    class func callPhone(_ phone:String){
        
        let phonePrompt = "telprompt://" + phone
        guard let url = URL.init(string: phonePrompt) else {
            return
        }
        UIApplication.shared.open(url)
    }
    
    /// 打开苹果商店对应的app
    /// - Parameter app: ~
    class func openAppStore(for appId:String = ""){
        
        let urlString = "itms-apps://itunes.apple.com/app/id\(appId)"
        guard let url = URL(string: urlString) else { return }
        let can = UIApplication.shared.canOpenURL(url)
        if can {
            UIApplication.shared.open(url, options: [:]) { (b) in
                
            }
        }
    }
    
    /// 弹出设置权限提示
    /// - Parameters:
    ///   - type: 提示类型
    ///   - viewController: 展示的控制器
    class func showSettingAlert(of type:SettingType,to viewController:UIViewController){
        
        let alert = UIAlertController.init(title: "提示", message: type.rawValue, preferredStyle: .alert)
        let act = UIAlertAction.init(title: "取消", style: .default)
        let jumpSettingAct = UIAlertAction.init(title: "设置", style: .default, handler: type.handler())
        alert.addAction(act)
        alert.addAction(jumpSettingAct)
        viewController.present(alert, animated: true)
    }
}

